package com.example.demo.test.lecture.algorithm;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class TowSum {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }
        return new int[0];
    }

    @Test
    public void test() {
        int[] nums = {2, 5, 7, 11, 15};
        int[] result = twoSum(nums, 9);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < result.length; i++) {
            if (i == result.length-1) {
                sb.append(result[i]);
                continue;
            }
            sb.append(result[i] + " : ");
        }
        System.out.println(sb.toString());
    }
}
